package ssi.actions;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import ssi.beans.Contact;
import ssi.tools.Tool;

public class ContactAction extends  Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		request.setCharacterEncoding("UTF-8");
		
		String target=null;
		Contact contact=(Contact)form;
		String name=contact.getName();
		String email=contact.getEmail();
		String text=contact.getText();
		ActionErrors actionErrors = new ActionErrors();
		
		if(!((name==null || name.equals("")) || (email==null || email.equals("")) || (text==null || text.equals("")))){
			if(Tool.checkEmailFormat(email)){
				Properties props = new Properties();
				props.put("mail.smtp.host", "smtp.gmail.com");
				props.put("mail.smtp.socketFactory.port", "465");
				props.put("mail.smtp.socketFactory.class",
						"javax.net.ssl.SSLSocketFactory");
				props.put("mail.smtp.auth", "true");
				props.put("mail.smtp.port", "465");
		 
				Session session = Session.getDefaultInstance(props,
					new javax.mail.Authenticator() {
						protected PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication("ssiproject2013@gmail.com","inzynieria2013");
						}
					});
				try {
					Message message = new MimeMessage(session);
					message.setFrom(new InternetAddress("test@x-schronisko.com"));
					message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("ssiproject2013@gmail.com"));
					message.setSubject("Formularz Kontaktowy");
					message.setText("Witaj,\nktoś skorzystał z formularza kontaktowego i wysłał ci wiadomość:\nOd: " + name +"\nEmail: " + email +"\nTreść wiadomości:\n " + text);			
					Transport.send(message);
					target="success";
				} catch (MessagingException e) {
					target="failure";
					actionErrors.add("error",
							new ActionMessage("error.sendMessage"));
					throw new RuntimeException(e);
				}
			}else{
				target="failure";
				actionErrors.add("error",
						new ActionMessage("error.emailFormat"));
			}
		}else{
			target="failure";
			actionErrors.add("error",
					new ActionMessage("error.emptyFields"));
		}
		if (actionErrors.size() != 0) {
			extracted(request, actionErrors);
			return new ActionForward(mapping.getInput());
		}
		
		return mapping.findForward(target);
	}
	
	private void extracted(HttpServletRequest request, ActionErrors actionErrors) {
		saveErrors(request, actionErrors);
	}
}
